package de.fhaugsburg.games.boardgames.connect4.consoleui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import de.fhaugsburg.games.boardgames.PlayerMode;
import de.fhaugsburg.games.boardgames.connect4.Connect4Engine;
import de.fhaugsburg.games.boardgames.connect4.strategy.RandomRobot;
import de.fhaugsburg.games.boardgames.exceptions.GameException;
import de.fhaugsburg.games.boardgames.exceptions.WrongCommandException;

/**
 * this is the central class in the game Connect4, from here all other classes were controlled.
 * 
 * @author r4smus
 * 
 */
public class CommandProcessor {

	private Connect4Engine engine;
	private BoardView boardView;
	private RandomRobot robot;

	/**
	 * the constructor.
	 * 
	 * @param engine
	 * @param boardView
	 */
	public CommandProcessor(Connect4Engine engine, BoardView boardView, RandomRobot robot) {
		this.engine = engine;
		this.boardView = boardView;
		this.robot = robot;
	}

	/**
	 * this method start a new Connect4 game, handle all inputs from the user and delegate the other classes who are involved in Connect4.
	 */
	public void process() throws IllegalArgumentException {
		String[] input;
		Command command = Command.EXIT;
		int column;

		while (true) {

			System.out.println("command: ");

			input = readCommand(); // reads the input from the user

			try {
				command = Command.valueOf(input[0].toUpperCase()); // command from the user, for exmaple NEWGAME, ADD, ...
			} catch (IllegalArgumentException e) {
				System.err.println("wrong command, please try again.");
				continue;
			}

			switch (command) {
			case NEWGAME:
				newgameOutput();
				engine.getConnect4Board().reset();
				engine.setPlayerMode(input[1].toUpperCase()); // sets the playerMode with a String, which has the value "SP" or "MP"
				boardView.render();
				break;

			case ADD:
				column = Integer.parseInt(input[1]) - 1; // sets the column from input at the index 1.

				try {
					engine.putPiece(column);

					if (engine.gameOver() == false)
						engine.changePiece();
					boardView.render();

					if (engine.getPlayerMode().equals(PlayerMode.SINGLE) && engine.gameOver() == false) {
						Thread.sleep(1000);
						engine.putPiece(robot.columnProposalSimple(engine.getCurrentPiece(), engine.getConnect4Board()));
						boardView.render();
						if (engine.gameOver() == false)
							engine.changePiece();

					}
				} catch (InterruptedException e) {
					e.printStackTrace();

				} catch (GameException e1) {
					System.err.println(e1.getMessage());
					continue;
				}
				break;

			case HELP:
				System.out.println("NEWGAME <SP | MP>            *start a newgame in the singleplayer- or multiplayermode");
				System.out.println("ADD columnnr                 *put a piece in a specific column number");
				System.out.println("HELP");
				System.out.println("EXIT                         *exit the game");
				break;

			case EXIT:
				System.out.println("exit the game...");
				System.exit(0);
				break;

			default:
				break;
			}
			try {
				if (engine.gameOver()) {
					System.out.println("the player with the piece " + engine.getCurrentPiece() + " wins the game");
					System.exit(0);
				}
			} catch (GameException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	private String[] readCommand() {
		String input = "";
		String[] inputRefine = new String[10];
		BufferedReader myinput = new BufferedReader(new InputStreamReader(System.in));
		try {
			input = myinput.readLine();
			inputRefine = input.split(" ");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return inputRefine;
	}

	private void newgameOutput() {
		System.out.println("------------------------------------------------------------------------------------------------------------------");
		System.out.println("----------------------------------------------------Connect 4-----------------------------------------------------");
		System.out.println("------------------------------------------------------------------------------------------------------------------");
	}

	private enum Command {
		NEWGAME, ADD, HELP, EXIT;

	}

}
